package com.example.myQuartz.controller;

import com.example.myQuartz.dao.UserMapper;
import com.example.myQuartz.entity.ResultData;
import com.example.myQuartz.entity.ScheduleJobEntity;
import com.example.myQuartz.entity.UserEntity;
import com.example.myQuartz.service.ScheduleJobService;
import com.example.myQuartz.task.ValidatorUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.UUID;


@RestController
@RequestMapping("/schedule")
public class ScheduleJobController {

    @Autowired
    private ScheduleJobService scheduleJobService;

    /**
     * 定时任务列表
     */
    @RequestMapping("/list")
    public ResultData list() {
        return scheduleJobService.listJob(1,10);
    }

    /**
     * 定时任务信息
     */
    @RequestMapping("/info/{jobId}")
    public ResultData info(@PathVariable("jobId") Long jobId) {
        ScheduleJobEntity schedule = scheduleJobService.queryObject(jobId);
        ResultData resultData = new ResultData();
        resultData.setData(schedule);

        return resultData;

    }

    /**
     * 保存定时任务 @RequestBody ScheduleJobEntity scheduleJob
     */
    @RequestMapping("/save")
    public ResultData save() {

        //先测试编写一个临时ScheduleJobEntity，毕竟后端页面不会写
        ScheduleJobEntity jobEntity = createJob();
        ValidatorUtils.validateEntity(jobEntity);

        scheduleJobService.save(jobEntity);

        return new ResultData();
    }

    /**
     * 修改定时任务
     */
    @RequestMapping("/update")
    public ResultData update(@RequestBody ScheduleJobEntity scheduleJob) {
        ValidatorUtils.validateEntity(scheduleJob);

        scheduleJobService.update(scheduleJob);
        return new ResultData();
    }

    /**
     * 删除定时任务
     */
    @RequestMapping("/delete")
    public ResultData delete(@RequestBody Long[] jobIds) {
        scheduleJobService.deleteBatch(jobIds);
        return new ResultData();
    }

    /**
     * 立即执行任务
     */
    @RequestMapping("/run")
    public ResultData run(@RequestBody Long[] jobIds) {
        scheduleJobService.run(jobIds);
        return new ResultData();
    }

    /**
     * 暂停定时任务
     */
    @RequestMapping("/pause")
    public ResultData pause(@RequestBody Long[] jobIds) {
        scheduleJobService.pause(jobIds);
        return new ResultData();
    }

    /**
     * 恢复定时任务
     */
    @RequestMapping("/resume")
    public ResultData resume(@RequestBody Long[] jobIds) {
        scheduleJobService.resume(jobIds);
        return new ResultData();
    }

    /**
     * 定时任务日志列表
     */
    @RequestMapping("/log/list")
    public ResultData listLog() {
       return scheduleJobService.listLog(1,10);
    }

    /**
     * 定时任务日志信息查询
     */
    @RequestMapping("/log/info/{logId}")
    public ResultData infoLog(@PathVariable("logId") Long logId) {
        return scheduleJobService.queryLogObject(logId);
    }

    public static ScheduleJobEntity createJob(){
        ScheduleJobEntity jobEntity = new ScheduleJobEntity();
        jobEntity.setBeanName("mytask");
        jobEntity.setCreateTime(new Date());
        jobEntity.setMethodName("firstTask");
        jobEntity.setCronExpression("0/10 * * * * ?");
        jobEntity.setParams(null);
        jobEntity.setStatus(0);
        jobEntity.setRemark("测试该定时任务的执行");
        return jobEntity;
    }
    @Autowired
    UserMapper userMapper;

    @GetMapping(value = "/test10")
    public String test10(){
        userMapper.save(new UserEntity(UUID.randomUUID().toString(),"chengjian"));
        return "SUCESS";
    }
}
